<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-16
 * Time: 21:00
 */

namespace App\Http\Controllers\Employee;

use App\Base\BaseFunc;
use App\Base\TaskState;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class EmployeeTaskController extends Controller
{
    public function searchAreaTask(TaskState $taskState)
    {
        $data['area'] = DB::table('area')
            ->where('area_id','=',session('employee_area'))
            ->first();
        $data['nowArea'] = $data['area']->area_name; //获取当前区域名称
        $taskStates = $taskState->unstate()->task_state_id; //获取未接受任务ID
        $data['areaData'] = DB::table('area')->get();
        $data['moneyData'] =DB::table('task')
            ->where('task_area','=',session('employee_area'))
            ->where('task_state','=',$taskStates)
            ->leftJoin('area','area_id','=','task_area')
            ->leftJoin('task_state','task_state_id','=','task_state')
            ->leftJoin('customer','customer_id','=','task_customer')
            ->get();

        return view("/employee.searchAreaTask",$data);
    }

    public function searchAreaTasks($task_id)
    {
        $data['employee'] = DB::table('employee_re_task')
            ->where('task_id','=',$task_id)
            ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
            ->first();   //获得领取当前任务的人
        if($data['employee'] == NULL)
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee_name'] = "暂无";
            $data['employee_phone'] = "暂无";
            return view('/employee.searchAreaTasks',$data);
        }
        else
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee'] = DB::table('employee_re_task')
                ->where('task_id','=',$task_id)
                ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
                ->first();   //获得领取当前任务的人
            $data['employee_name'] = $data['employee']->employee_name;
            $data['employee_phone'] = $data['employee']->employee_phone;
            return view('/employee.searchAreaTasks',$data);
        }
    }

    public function employeeToTask(BaseFunc $baseFunc,TaskState $taskState)
    {
        $input = Input::only('task_id');
        $input['employee_id'] = session('employee_id');
        $newState['task_state'] = $taskState->stating()->task_state_id;
        $stating = DB::table('employee_re_task')->insert($input);
        if($stating)
        {
            DB::table('task')->where('task_id','=',$input['task_id'])->update($newState);
            $baseFunc->setRedirectMessage(true,"任务领取成功！",NULL,NULL);
            return redirect()->back();
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"任务领取失败！",NULL,NULL);
            return redirect()->back();
        }
    }

    public function searchMyTask(TaskState $taskState)
    {
        $data['area'] = DB::table('area')
            ->where('area_id','=',session('employee_area'))
            ->first();
        $data['nowArea'] = $data['area']->area_name; //获取当前区域名称

        $data['myTask'] = DB::table('employee_re_task')
            ->where('employee_id',"=",session('employee_id'))
            ->leftJoin('task','task.task_id','=','employee_re_task.task_id')
            ->leftJoin('task_state','task_state.task_state_id',"=",'task.task_state')
            ->get();
        return view("/employee.searchMyTask",$data);
    }

    public function searchMyTasks($task_id)
    {
        $data['employee'] = DB::table('employee_re_task')
            ->where('task_id','=',$task_id)
            ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
            ->first();   //获得领取当前任务的人
        $data['evaluationData'] = DB::table('evaluation')
            ->where('task_id','=',$task_id)
            ->leftJoin('customer','customer.customer_id','=','evaluation.customer_id')
            ->get();
        if($data['employee'] == NULL)
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee_name'] = "暂无";
            $data['employee_phone'] = "暂无";
            return view('/employee.searchMyTasks',$data);
        }
        else
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->leftJoin('task_state','task_state.task_state_id',"=",'task.task_state')
                ->first();  //获得当前任务
            $data['employee'] = DB::table('employee_re_task')
                ->where('task_id','=',$task_id)
                ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
                ->first();   //获得领取当前任务的人
            $data['employee_name'] = $data['employee']->employee_name;
            $data['employee_phone'] = $data['employee']->employee_phone;
            return view('/employee.searchMyTasks',$data);
        }
    }

    public function complateTask(BaseFunc $baseFunc,TaskState $taskState)
    {
        $input = Input::only('task_id');
        $newState['task_state'] = $taskState->stated()->task_state_id;
        DB::table('task')->where('task_id','=',$input['task_id'])->update($newState);
        $baseFunc->setRedirectMessage(true,"恭喜！任务完成！",NULL,"/employee.searchMyTask");
    }
}